#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>

/**
 * printf打印：
 * （打印最多50字节长度的字符串）
 * printf("[%.*s]]\r", 50, arr);
 * 
 * （打印至少100字节的字符串，且左对齐，超过则继续打印。若需要右对齐则不要'-'符号）
 * printf("[%-100s]]\r", arr);
 * 
 * for (int i=1; i<100; i++) {
        char buff[50];
        sprintf(buff, "[%%-%ds]\r\0\0\0", i);
        printf(buff, "=====");
        fflush(stdout);
        sleep(1);
    }   //动态修改中括号间的距离
 * 
 * printf("%-*.2f", 20, 20.564);    打印至少20字节的字符串，且左对齐,超过则继续打印
 * printf("%-*d", 20, 20); 
 */

int GetWinsize(int *row, int *column) {
    struct winsize w;  
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {  
        perror("ioctl TIOCGWINSZ");  
        return 1;  
    }
    *row = w.ws_row;
    *column = w.ws_col;
    // printf("Rows: %d, Columns: %d\n", w.ws_row, w.ws_col);  
    return 0;
}

void CursorMoveUpOneRow() {
    printf("\033[A");
}

void CursorMoveDownOneRow() {
    printf("\033[B");
}

void CursorMoveRightOneColumn() {
    printf("\033[C");
}

void CursorMoveLeftOneColumn() {
    printf("\033[D");
}

void CursorMoveToColumn(int n) {
    printf("\033[%dG", n);
}

void CursorMoveToPoint(int row, int column) {
    printf("\033[%d;%dH", row, column);
}

void CursorMoveToLeftTop() {
    printf("\033[H");
}

void CursorSavePoint() {
    printf("\033[s");
}

void CursorRestorePoint() {
    printf("\033[u");
}

void ClearWindows() {
    printf("\033[2J");
}

void ClearAfterCursor() {
    printf("\033[0J");
}

void ClearBeforeCursor() {
    printf("\033[1J");
}

void CursorHide() {
    printf("\033[?25l");
}

void CursorShow() {
    printf("\033[?25h");
}

void ClearLineAfterCursor() {
    printf("\033[K");
    fflush(stdout);
}

void RollingUpOneLine() {
    printf("\033[S");
}

void RollingDownOneLine() {
    printf("\033[T");
}

void SetColorStyle(int ct) {
    printf("\033[%dm", ct);
}

void CursorMoveToLeftBeginAndClearAfter() {
    printf("\r\033[K");
    fflush(stdout);
}

int main1(int argc, char **argv) {
    int i,j;
    GetWinsize(&i, &j);
    SetColorStyle(5);
    ClearLineAfterCursor();
    sleep(2);
    printf("===");
    sleep(2);
    int input = 0;
    while (1) {
        printf("input your cmd:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            CursorMoveUpOneRow();
            break;
        case 2:
            CursorMoveDownOneRow();
            break;
        case 3:
            CursorMoveRightOneColumn();
            break;
        case 4:
            CursorMoveLeftOneColumn();
            break;
        case 5:
            CursorMoveToLeftTop();
            break;
        case 6:
            CursorSavePoint();
            break;
        case 7:
            CursorRestorePoint();
            break;
        case 8:
            ClearWindows();
            break;
        case 9:
            ClearAfterCursor();
            break;
        case 10:
            ClearBeforeCursor();
            break;
        case 11:
            CursorHide();
            break;
        case 12:
            CursorShow();
            break;
        case 13:
            ClearLineAfterCursor();
            break;
        case 14:
            RollingUpOneLine();
            break;
        case 15:
            RollingDownOneLine();
            break;
        case 16:
            printf("input your ct:\n");
            scanf("%d", &input);
            SetColorStyle(input);
            break;
        case 17:
            printf("input your row:\n");
            scanf("%d", &i);
            printf("input your column:\n");
            scanf("%d", &j);
            CursorMoveToPoint(i, j);
            break;
        default:
            break;
        }
    }

    return 0;
}

